﻿ 
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
 
using System.Drawing.Imaging;

namespace MOKA.TPM.Utility
{
    public static class Utility
    {
 

        public static int ObjectToInt(this object value)
        {
            if (value is DBNull || value == null)
            {
                return 0;
            }
            return Convert.ToInt32(value);
        }

        public static DateTime? ObjectToDateTime(this object value)
        {
            if (value is DBNull || value == null)
            {
                return null;
            }
            return Convert.ToDateTime(value);
        }

        public static double ObjectToDouble(this object value)
        {
            if (value is DBNull)
                return 0;
            return Convert.ToDouble(value);
        }

        public static string ObjectToString(this object value, bool isOnlyDate = false)
        {
            if (value is DBNull || value == null)
            {
                return "";
            }

            if (value is DateTime)
            {
                if (isOnlyDate)
                {
                    return Convert.ToDateTime(value).ToString("yyyy-MM-dd");
                }
                return Convert.ToDateTime(value).ToString("yyyy-MM-dd HH:mm:ss");
            }
            return value.ToString();
        }

        /// <summary>
        /// 图片压缩
        /// </summary>
        /// <param name="sFile">原始文件路径</param>
        /// <param name="outPath">存放图片路径</param>
        /// <param name="quality">质量压缩比例(1-100)</param>
        /// <returns></returns>
        public static bool GetPicThumbnail(string sFile, int quality, ref string outPath)
        {
            System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
            try
            {
                ImageFormat tFormat = iSource.RawFormat;

                //以下代码为保存图片时，设置压缩质量  
                EncoderParameters ep = new EncoderParameters();
                long[] qy = new long[1];
                qy[0] = quality;//设置压缩的比例1-100  
                EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
                ep.Param[0] = eParam;
                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegICIinfo = null;
                for (int x = 0; x < arrayICI.Length; x++)
                {
                    if (arrayICI[x].FormatDescription.Equals("JPEG"))
                    {
                        jpegICIinfo = arrayICI[x];
                        break;
                    }
                }
                if (jpegICIinfo != null)
                {
                    iSource.Save(outPath, jpegICIinfo, ep);//dFile是压缩后的新路径  
                }
                else
                {
                    iSource.Save(outPath, tFormat);
                }
                return true;
            }
            catch
            {
                //图片压缩失败，则按原文件
                outPath = sFile;
                return false;
            }
            finally
            {
                iSource.Dispose();
                iSource.Dispose();
            }
        }


        /// <reutrn>返回第⼏周</reutrn>
        public static int GetWeekOfYear(DateTime dt)
        {
            System.Globalization.GregorianCalendar gc = new System.Globalization.GregorianCalendar();
            int weekOfYear = gc.GetWeekOfYear(dt, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
            return weekOfYear;
        }

    }
}